package com.base.myproject.client.busi;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.base.myproject.client.ui.BaseJDBCPanel;
import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.data.BaseListLoader;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.HttpProxy;
import com.extjs.gxt.ui.client.data.JsonReader;
import com.extjs.gxt.ui.client.data.ListLoadResult;
import com.extjs.gxt.ui.client.data.LoadEvent;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.data.XmlReader;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.LoadListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.EditorGrid;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.table.NumberCellRenderer;
import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class TestBusi extends BaseJDBCPanel {
	
	public void init()
	{
		
		Html html = new Html("aaaaaaaa");
		ListStore dd = new ListStore();
		BaseModelData bmd = new BaseModelData();
		HashMap ht = new HashMap();
		
		ht.put("col1", "zgw");
		bmd.setProperties(ht);
		dd.add(bmd);
		bmd.getProperties().get("col1");
		System.out.println(bmd.getProperties().get("col1"));
		//com.extjs.gxt.samples.resources.client.TestData tt = new TestData();
		this.add(html);
		
//		System.out.println("grid="+createGrid());
//		try {
//			testservlet();
//		} catch (RequestException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		ModelType mt = new ModelType(); 
	
//		mt.setRoot("Records"); 
//		mt.setTotalName("TotalRecords"); 
		mt.addField("gcode"); 
		mt.addField("gname"); 
		//mt.addField("symbol"); 
	//	ListStore store = JsonStoreCreatePaginate("ds01",mt,"TestServlet?name=aaa&age=13","id");
//		 grid = createGrid();
//		 System.out.println("grid==null ="+(grid==null));
		getserverdate();
//		 System.out.println("grid==null ="+(grid==null));
		
	//	 this.add(grid);
		//this.add(XmlGridExample());
		super.init();
	}
	
private void testservlet() throws RequestException
{
	String url = "TestServlet"  ;
		RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,URL.encode(url));  
		builder.setHeader("Content-Type", "application/x-www-form-urlencoded");  
		String form = "name=aaa&age=13";  
		builder.sendRequest(form,new RequestCallback() {

			@Override
			public void onError(Request request, Throwable exception) {
				// TODO Auto-generated method stub
				System.out.println(" send servlet error");
			}

			@Override
			public void onResponseReceived(Request request, Response response) {
				// TODO Auto-generated method stub
				System.out.println(" send servlet success"+ response.getText()+"--");
			}  
		    // ͬ��GET�����ʵ��  
		});  
	
}

	 private EditorGrid createGrid() {  
		    final NumberFormat currency = NumberFormat.getCurrencyFormat();  
		    final NumberFormat number = NumberFormat.getFormat("0.00");  
		    final NumberCellRenderer numberRenderer = new NumberCellRenderer(currency);  
		  
		    GridCellRenderer change = new GridCellRenderer() {  
		 

			@Override
			public Object render(ModelData model, String property,
					ColumnData config, int rowIndex, int colIndex,
					ListStore store, Grid grid) {
				 double val = (Double)model.get(property);  
			        String style = val < 0 ? "red" : "green";  
			        return "<span style='color:" + style + "'>" + number.format(val) + "</span>";  
			}  
		    };  
		  
		    GridCellRenderer gridNumber = new GridCellRenderer() {  
		    

			@Override
			public Object render(ModelData model, String property,
					ColumnData config, int rowIndex, int colIndex,
					ListStore store, Grid grid) {
				// TODO Auto-generated method stub
				  return numberRenderer.render(null, property, model.get(property));
			}  
		    };  
		  
		    List<ColumnConfig> configs = new ArrayList<ColumnConfig>();  
		  
		    ColumnConfig column = new ColumnConfig();  
		    column.setId("name");  
		    column.setHeader("Company");  
		    column.setWidth(200);  
		    
		    TextField<String> text = new TextField<String>(); 
		    column.setEditor(new CellEditor(text));
		    configs.add(column);  
		    
		    column = new ColumnConfig();  
		    column.setId("symbol");  
		    column.setHeader("Symbol");  
		    column.setWidth(75);  
		    configs.add(column);  
		  
		    column = new ColumnConfig();  
		    column.setId("last");  
		    column.setHeader("Last");  
		    column.setAlignment(HorizontalAlignment.RIGHT);  
		    column.setWidth(75);  
		    column.setRenderer(gridNumber);  
		    configs.add(column);  
		  
		    column = new ColumnConfig("change", "Change", 90);  
		    column.setAlignment(HorizontalAlignment.RIGHT);  
		  //  column.setRenderer(change);  
		    configs.add(column);  
		  
		    column = new ColumnConfig("date", "Last Updated", 90);  
		    column.setAlignment(HorizontalAlignment.RIGHT);  
		    column.setDateTimeFormat(DateTimeFormat.getShortDateFormat());  
		    configs.add(column);  
		  
		  
			
			//-------------------
		   // store.add(TestData.getStocks());  
		     ColumnModel cm = new ColumnModel(configs);  
		    
		    BaseStore store = new BaseStore();  
			//---------
//			BaseStore store = new BaseStore();  
		    store.setMonitorChanges(true);  
		    //--------------------
		    BaseModelData bmd = new BaseModelData();
			HashMap<String,Object> ht2 = new HashMap<String,Object>();
			
			ht2.put("name", "zgw");
			ht2.put("Company", "zgw");
			ht2.put("symbol", "zgw");
			ht2.put("last", "zgw");
			ht2.put("change", "zgw");
			bmd.setProperties(ht2);
			store.add(bmd);
			
		    //------------
		    
		    
		    EditorGrid grid=new EditorGrid(store, cm);
		    
		     //grid = new EditorGrid(store, cm);  
		   
		    grid.getView().setEmptyText("sdfd");  
		    grid.setBorders(false);  
		    grid.setAutoExpandColumn("name");  
		    grid.setBorders(true);  
		    System.out.println("---"+grid.toString());
		    return grid;  
		  }  

	 static ServiceAsync Service2 =GWT
		.create(Service.class);

	private void getserverdate()
	{
	
		System.out.println("Service=="+(Service2==null));
		Service2.getdate("zzz", new AsyncCallback<String>()
				{
				
			public void onFailure(Throwable caught) {
				System.err.println("failure");
			}

			@Override
			public void onSuccess(String result1) {
//				System.out.println("success!"+result1.getCount());
//				System.out.println("grid"+(grid==null));
//				System.out.println("cm="+(cm==null));
//				//grid.reconfigure(result, cm);
//	
//				grid = new EditorGrid(result1, cm);  
//				grid.recalculate();
		//		basepanel.add(result1);
				
			}
				});
		
	}
	
	static public ListStore<ModelData> JsonStoreCreatePaginate(String symbol,ModelType mt, String url, String sordField){ 
		RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url); 
		HttpProxy proxy = new HttpProxy(builder); 
		 
		// ------ json decoder -----------// 
		// basic load without paginate // 
		/*  ����㲻Ҫ���퓾����@���O�������ˡ�
		JsonReader<ListLoadResult<ModelData>> reader = new JsonReader<ListLoadResult<ModelData>>(mt);
		BaseListLoader<ListLoadResult<ModelData>> loader = new BaseListLoader<ListLoadResult<ModelData>>(proxy, reader);
		*/ 
		// ------ end json decoder -----------// 
		 
		JsonReader<ListLoadResult<ModelData>> reader = new JsonReader<ListLoadResult<ModelData>>(mt); 
//		{ 
//		@Override
//		protected ListLoadResult<ModelData> newLoadResult(Object loadConfig, 
//		List<ModelData> models) { 
//		PagingLoadConfig pagingConfig = (PagingLoadConfig) loadConfig; 
//		PagingLoadResult<ModelData> result = new BasePagingLoadResult<ModelData>(models, 
//		pagingConfig.getOffset(), pagingConfig.getLimit()); 
//		return result; 
//		} 
//		}; 
		BaseListLoader loader = new BaseListLoader(proxy, reader); 
		 
		loader.addLoadListener(new LoadListener() { 
		public void loaderBeforeLoad(LoadEvent le) { 
		Info.display("JData","Staring Loading...."); 
		//Window.alert("before load"); 
		} 
		public void loaderLoad(LoadEvent le) { 
		Info.display("JData","Loading complete!"); 
		
		} 
		 
		public void loaderLoadException(LoadEvent le) { 
		Info.display("JData","Loading Error!"+le.exception); 
		System.out.println(le.exception);
		} 
		}); 
		 
		//loader.setSortField(sordField); 
		//loader.setRemoteSort(true); 
	//	ListStore<ModelData> store = new ListStore<ModelData>(loader); 
		//Registry.register(symbol, store); 
		loader.load(); // load Store 
	//	System.out.println("count="+store.getCount());
	//	return store; 
		return null;
		} 
	  public ContentPanel initWeiget() {  
		    setLayout(new FlowLayout(10));  
		    CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		    List<ColumnConfig> columns = new ArrayList<ColumnConfig>();  
		    columns.add(cbs.getColumn());
		    ColumnConfig column = new ColumnConfig("Sender", "gcode2", 100); 
		    cbs.setSelectionMode(Style.SelectionMode.MULTI);
		    
		    
		    TextField<String> text = new TextField<String>(); 
		    column.setEditor(new CellEditor(text));
		  
		    columns.add(column);
		    //columns.add(new ColumnConfig("Sender", "gcode2", 100));  
		    columns.add(new ColumnConfig("gname", "Email", 165));  
		    columns.add(new ColumnConfig("Phone", "Phone", 100));  
		    columns.add(new ColumnConfig("State", "State", 50));  
		    columns.add(new ColumnConfig("Zip", "Zip Code", 65));  
		  
		    // create the column model  
		    ColumnModel cm = new ColumnModel(columns);  
		  
		    // defines the xml structure  
		    ModelType type = new ModelType();  
		    type.setRoot("records");  
		    type.setRecordName("record");   
		    type.addField("Sender", "gcode");  
		    type.addField("gname");  
		    type.addField("Phone");  
		    type.addField("State");  
		    type.addField("Zip");  
		  
		    // use a http proxy to get the data  
		    RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, "TestServlet?name=ee");  
		    HttpProxy proxy = new HttpProxy(builder);  
		  
		    // need a loader, proxy, and reader  
		    XmlReader reader = new XmlReader(type);  
		  
		    final BaseListLoader loader = new BaseListLoader(proxy, reader);  
		  
		    ListStore<ModelData> store = new ListStore<ModelData>(loader);  
		    final  EditorGrid grid=new EditorGrid(store, cm);
		    //store.getAt(index)
		    grid.setSelectionModel(cbs);
		    grid.setBorders(true);  
		    grid.setAutoExpandColumn("Sender");  
		  
		    ContentPanel panel = new ContentPanel();  
		    panel.setFrame(true);  
		    panel.setCollapsible(true);  
		    panel.setAnimCollapse(false);  
		    panel.setButtonAlign(HorizontalAlignment.CENTER);  
		    panel.setIconStyle("icon-table");  
		    panel.setHeading("XML Table Demo");  
		    panel.setLayout(new FitLayout());  
		    panel.add(grid);  
		    panel.setSize(575, 350);  
		  
		    // add buttons  
		    Button load = new Button("Load XML");  
		   // loader.setReuseLoadConfig(true);
		    boolean b= loader.load();  
		    load.addSelectionListener(new SelectionListener<ButtonEvent>() {  
		      public void componentSelected(ButtonEvent ce) {  
		     
		        System.out.println("load=");
		        
		      }  
		    });  
		    
		    loader.addLoadListener(new LoadListener() { 
				public void loaderBeforeLoad(LoadEvent le) { 
				Info.display("JData","Staring Loading...."); 
				//Window.alert("before load"); 
				} 
				public void loaderLoad(LoadEvent le) { 
				Info.display("JData","Loading complete!"); 
				
				} 
				 
				public void loaderLoadException(LoadEvent le) { 
				Info.display("JData","Loading Error!"+le.exception); 
				System.out.println(le.exception);
				
				} 
				}); 
		    panel.addButton(new Button("Reset", new SelectionListener<ButtonEvent>() {  
		    	       @Override  
		    	       public void componentSelected(ButtonEvent ce) {  
		    	         grid.getStore().rejectChanges();  
		    	       
		    	       }  
		    	     }));
		    panel.addButton(new Button("Commit", new SelectionListener<ButtonEvent>() {  
		    	       @Override  
		    	       public void componentSelected(ButtonEvent ce) {  
		    	    	   System.out.println("beforesave="+grid.getStore().getAt(0).get("Sender"));
		    	    	//   grid.getStore().removeAll();
		    	        // grid.getStore().commitChanges(); 
		    	    	  
		    	    	   grid.getStore().remove(grid.getSelectionModel().getSelectedItem());
		    	        System.out.println("aftersave="+grid.getStore().getAt(0).get("Sender"));
		    	       }  
		    	    }));  
		    panel.addButton(load);  
		  //  add(panel);  
		   
		  return panel;
		  }  
}
